package com.cheetah.start.module.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheetah.start.common.base.query.PageQuery;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultTable;
import com.cheetah.start.module.system.domain.SysSet;
import com.cheetah.start.module.system.service.ISysSetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.constraints.NotBlank;

/**
 * 系统设置控制器
 *
 * @author xjh
 * @date 2022/04/22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/system/set")
public class SysSetController extends ResultController {

    @Autowired
    private ISysSetService sysSetService;

    @GetMapping()
    public ModelAndView listView() {
        return jumpPage("/system/set/list");
    }

    @GetMapping("/add")
    public ModelAndView addView() {
        return jumpPage("/system/set/add");
    }

    @GetMapping("/edit")
    public ModelAndView edit(ModelAndView modelAndView, @NotBlank(message = "key不能为空") String setKey) {
        SysSet byKey = sysSetService.getByKey(setKey, false);
        modelAndView.addObject("sysSet", byKey);
        modelAndView.setViewName("/system/set/edit");
        return modelAndView;
    }


    @GetMapping("/list")
    public ResultTable list(PageQuery pageQuery, SysSet sysSet) {
        Page<SysSet> pageInfo = sysSetService.pageList(pageQuery, sysSet);
        return pageTable(pageInfo.getRecords(), pageInfo.getTotal());
    }


    @PostMapping("/save")
    public Result save(SysSet sysSet) {
        return success(sysSetService.saveOrUpdateByKey(sysSet));
    }


    @PostMapping("/delete")
    public Result deleteByKey(@NotBlank(message = "key不能为空") String key) {
        return decide(sysSetService.removeByKey(key));
    }

}
